package jihe_set_of_APIdemo;

/**
 * @author ChenYongHao
 * @date 2023/4/28 10:05
 * @desc TreeSet演示：按照年龄从小到大排序，年龄相同时，按照姓名的字母顺序排序
 */
import java.util.Collection;
import java.util.Comparator;
import java.util.TreeSet;

class StudentType {
    private String name;
    private Integer age;

    public StudentType() {
        super();
    }

    public StudentType(String name, Integer age) {
        super();
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student [name=" + name + ", age=" + age + "]";
    }
}

public class Set_TreeSet_ApiDemo {
    public static void main(String[] args) {
        // 创建集合
        Collection<StudentType> collection = new TreeSet<StudentType>(new Comparator<StudentType>() {
            @Override
            public int compare(StudentType s1, StudentType s2) {
                // 主要条件
                int sort1 = s1.getAge() - s2.getAge();
                // 次要条件
                int sort2 = sort1 == 0 ? s1.getName().compareTo(s2.getName()) : sort1;
                return sort2;
            }
        });

        // 添加元素
        collection.add(new StudentType("王五", 22));
        collection.add(new StudentType("王五", 22));
        collection.add(new StudentType("张三1", 20));
        collection.add(new StudentType("张三0", 20));
        collection.add(new StudentType("李四0", 18));
        collection.add(new StudentType("李四1", 18));
        System.out.println(collection.toString());
    }
}
